/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaboken;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

/**
 *
 * @author Bertil
 *
 * Finns någon bugg jag lyckats skapa för mig själv då programmet fungerade ett tag. inte längre.
 *
 *
 */
public class kap28 {
    public static void kap28(){
        timeDisplayer k = new timeDisplayer();
    }
}
class timeDisplayer extends JFrame implements ActionListener{
    private pointInTime tp = new pointInTime();
    private JLabel text;

    public timeDisplayer(){
        javax.swing.Timer hr = new javax.swing.Timer(1000, this);
        hr.start();

        Calendar cal = Calendar.getInstance();

        tp.set(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));

        text = new JLabel(tp.toString(), JLabel.CENTER);
        add(text);
        text.setOpaque(true);
        text.setBackground(Color.black);
        text.setForeground(Color.white);
        text.setFont(new Font("Serif", Font.BOLD, 24));
        setSize(200, 75);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e){

    tp.tick();
    text.setText(tp.toString());
    }
}

class pointInTime{
    private int h, m, s;
    private boolean showSec = true;

    public void set(int hr, int min, int sec){
        if(hr >= 0 &&  hr < 24 && min >= 0 && min < 60 && sec >= 0 && sec < 60){
            h = hr;
            m = min;
            s = sec;
        }
        else{
            System.out.println("Invalid point in time");
        }
    }

    public void setShowSec(boolean show){
        showSec = show;
    }

    public int readHr(){
        return h;
    }

    public int readMin(){
        return m;
    }

    public int readSec(){
        return s;
    }

    @Override
    public String toString(){
        String time = h + ":" + m;
        if(showSec){
            time = time + ":" + s;
        }
        return time;
    }
    
    public void tick(){
    s = s+1;
        if(s == 60){
            s = 0;
            m = m++;
        }
        if(m == 60){
            m = 0;
            h = h++;
        }
        if(h == 24){
            h = 0;
        }
    }
}
